﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
namespace AlbonWeb.Manager.MenuManager
{
    public partial class MenuManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["limit"] = " where 1=1";
                //父级菜单
                string cmdstr = "select * from Menu where parentID=0";
                SqlHelper.BindDropDownList(ddlTop, "menuName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                ddlTop.Items.Insert(0, "--top menu--");
                //子菜单
                bindRep();
            }
        }
        //绑定菜单数据
        protected void bindRep()
        {
            string cmdstr = "";
            if (menuName.Text.Trim() != "")
            {
                ViewState["limit"] += " and menuName like '%"+menuName.Text.Trim()+"%'";
            }
            if (ddlTop.SelectedIndex != 0)
            {
                ViewState["limit"] += " and parentID="+ddlTop.SelectedValue;
            }
            cmdstr = "select count(*) from MenuView"+ViewState["limit"].ToString();
            AspNetPager1.RecordCount=int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null).ToString());
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@tablename","MenuView"),
                new SqlParameter("@startIndex",AspNetPager1.StartRecordIndex),
                new  SqlParameter("@endIndex",AspNetPager1.EndRecordIndex),
                new SqlParameter("@limit",ViewState["limit"].ToString()),
                new SqlParameter("@orderby","sequence asc")

            };
            SqlHelper.BindRepeater(repMain, SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "netpager", parameters);
        }

        //换页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            bindRep();
        }

        //搜索
        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            ViewState["limit"] = " where 1=1";
            bindRep();
        }

        //添加新菜单项
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string path = "/Manager/MenuManager/MenuAdd.aspx?pid="+ddlTop.SelectedValue;
            if (menuName.Text.Trim() != "")
            {
                path += "&menuname="+menuName.Text.Trim();
            }
            Response.Redirect(path);
        }

        protected void ddlTop_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["limit"] = " where 1=1";
            bindRep();
        }
    }
}